package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());
        //Long转localDateTime
        if(deviceDataPageReqDto.getStartTime()!=null){
            deviceDataPageReqDto.setStartDate(LocalDateTime.ofInstant(new Date(deviceDataPageReqDto.getStartTime()).toInstant(), ZoneId.systemDefault()));
        }
        if(deviceDataPageReqDto.getEndTime()!=null){
            deviceDataPageReqDto.setEndDate(LocalDateTime.ofInstant(new Date(deviceDataPageReqDto.getEndTime()).toInstant(), ZoneId.systemDefault()));
        }
        Page<DeviceDataVo> deviceDataVos = deviceDataMapper.selectByPage(deviceDataPageReqDto);
        return PageResponse.of(deviceDataVos, DeviceDataVo.class);
    }
}
